package mysql

import (
	"user-goods-hub/userrpc/userrpc"

	"gorm.io/gorm"
)

type Goods struct {
	gorm.Model
	GoodsName  string `gorm:"type:varchar(30);not null"`
	GoodsPrice int    `gorm:"type:int(11);not null"`
	GoodsStock int    `gorm:"type:int(11);not null"`
	UserId     int    `gorm:"type:int(11);not null"`
}

func (g *Goods) GoodsCreate(db *gorm.DB) error {
	return db.Debug().Create(g).Error
}

func GoodsList(db *gorm.DB, id int64) []*userrpc.GoodsList {
	var lists []*userrpc.GoodsList
	db.Debug().Model(&Goods{}).Select(" goods.goods_name, goods.goods_price, goods.goods_stock, users.id as user_id").
		Joins("LEFT JOIN users ON goods.user_id = users.id").
		Where("users.id=?", id).Find(&lists)
	return lists
}
